package smecta.client.print.controller;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import smecta.client.print.domain.ScpCodeFormModel;
import smecta.client.print.domain.ScpDataAcqModel;
import smecta.client.print.service.ScpCodeFormService;
import smecta.client.print.service.ScpDataAcqService;
import smecta.client.print.util.AlertUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;

@RestController
public class CreateNewController {

    @FXML
    private Button confirmCloseButton;
    @FXML
    private Button confirmButton;
    ConcurrentHashMap<String, Object> cacheMap = ScpDataAcqController.cacheMap;

    @Autowired
    private ScpDataAcqService scpDataAcqService;
    @Autowired
    private ScpCodeFormService scpCodeFormService;
    @FXML
    private void handleConfirm() {
        saveItems();
        TableView tableView = (TableView) cacheMap.get("tableView");
        TextArea textArea2 = (TextArea) cacheMap.get("isUse");
        TextArea textArea1 = (TextArea) cacheMap.get("textArea1");

        textArea2.setText("");
        textArea1.setText("");
        tableView.getItems().clear();
        Stage stage = (Stage) confirmButton.getScene().getWindow();
        stage.close();
    }

    public void saveItems(){
        TableView tableView = (TableView) cacheMap.get("tableView");
        TextArea textArea2 = (TextArea) cacheMap.get("isUse");
        TextArea textArea1 = (TextArea) cacheMap.get("textArea1");
        TextField inspectionPersonTextField = (TextField) cacheMap.get("inspectionPersonTextField");
        List<ScpDataAcqModel> scpDataAcqModels = new ArrayList<>(tableView.getItems());
        if (CollectionUtils.isEmpty(scpDataAcqModels)){
            return;
        }

        String codeNum = textArea1.getText();
        String typeStr = textArea2.getText();

        if (StringUtils.isNotBlank(codeNum)){
            scpDataAcqService.deleteByCodeNum(codeNum);
        }else {
            codeNum = makeNextCodeNum();

            Integer type = 0;
            if ("定重".equals(typeStr)){
                type = 1;
            }else if ("配重".equals(typeStr)){
                type = 2;
            }else {
                typeStr = "不定重";
            }

            ScpCodeFormModel scpCodeFormModel = new ScpCodeFormModel();
            scpCodeFormModel.setCodeNum(codeNum);
            scpCodeFormModel.setType(type);
            scpCodeFormService.insertSelective(scpCodeFormModel);
        }
        for (ScpDataAcqModel model : scpDataAcqModels) {
            model.setCodeNum(codeNum);
            model.setCreator(inspectionPersonTextField.getText());
        }
        scpDataAcqService.insertSelectiveList(scpDataAcqModels);

        textArea1.setText(codeNum);
        textArea2.setText(typeStr);
    }

    public String makeNextCodeNum(){
        // TODO 从初始设置中获取 判定第二天的条件
        TextField inspectionPersonTextField = (TextField) cacheMap.get("inspectionPersonTextField");
        DatePicker datePicker = (DatePicker) cacheMap.get("datePicker");
        SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
        LocalDate localDate = datePicker.getValue();
        Date date = new Date(localDate.atStartOfDay(TimeZone.getDefault().toZoneId()).toInstant().toEpochMilli());
        String codeNum = format.format(date) + inspectionPersonTextField.getText();
        String maxCodeNum = scpCodeFormService.getMaxCodeNum(codeNum);
        if (StringUtils.isNotBlank(maxCodeNum)){
            codeNum = String.valueOf(Long.parseLong(maxCodeNum) + 1L);
        }else {
            codeNum = codeNum + "001";
        }
        return codeNum;
    }
    @FXML
    private void handleCancel() {
        // 关闭窗口
        Stage stage = (Stage) confirmCloseButton.getScene().getWindow();
        stage.close();
    }

}
